package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lanlian.cayman.R;
import com.lanlian.utillib.DisplayUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2019/1/21 19:03
 *     desc   : 开户选择账户类型
 *     version: 1.0
 * </pre>
 */
public class AccountTypeDialog extends Dialog implements View.OnClickListener {

    private ImageView mImgClose;
    private RadioGroup mRadioGroup;
    private RadioButton mRadioAll;
    private RadioButton mRadioStandard;

    private Context mContext;

    private int mAccountType;
    private OnChooseListener mOnChooseListener;

    public interface OnChooseListener {
        void onChoose(int accountType);
    }

    public AccountTypeDialog(@NonNull Context context, OnChooseListener onChooseListener, int accountType) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mOnChooseListener = onChooseListener;
        this.mAccountType = accountType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_account_type);
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = (int) (DisplayUtil.getScreenWidth(mContext) * 0.8);
        initView();
        if (mAccountType == 0) {
            mRadioAll.setChecked(true);
        } else {
            mRadioStandard.setChecked(true);
        }
        setListeners();
    }

    private void initView() {
        mImgClose = findViewById(R.id.img_close);
        mRadioGroup = findViewById(R.id.radio_group);
        mRadioAll = findViewById(R.id.radio_all);
        mRadioStandard = findViewById(R.id.radio_standard);
    }

    private void setListeners() {
        mImgClose.setOnClickListener(this);
        mRadioAll.setOnClickListener(this);
        mRadioStandard.setOnClickListener(this);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_all:
                        mOnChooseListener.onChoose(0);
                        break;
                    case R.id.radio_standard:
                        mOnChooseListener.onChoose(1);
                        break;
                    default:
                        break;
                }
                dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_close:
                dismiss();
                break;
            case R.id.radio_all:
                if (mRadioAll.isChecked()) {
                    mOnChooseListener.onChoose(0);
                    dismiss();
                }
                break;
            case R.id.radio_standard:
                if (mRadioStandard.isChecked()) {
                    mOnChooseListener.onChoose(1);
                    dismiss();
                }
                break;
            default:
                break;
        }
    }

}
